<?php

namespace app\index\controller;

use app\commons\model\Member;
use app\commons\model\MemberVideo;
use app\index\controller\Main;
use douyin\Dy;
use think\Controller;
use GuzzleHttp\Client;
use think\Db;
use think\exception\ValidateException;

class Videom extends Main {

    public function index() {

        $_GPC = input();
        $page = max(intval($_GPC['page']), 1);
        $limit = max(intval($_GPC['limit']), 10);
        $map = [];
        if ($this->merchinfo['isagent']) {
            if ($this->sysset['agent_level'] != '') {
                if ($this->sysset['agent_level'][$this->merchinfo['agent_level']]['type'] == 0) {
                    $map['m.agent_id'] = $this->merchinfo['id'];
                }
                if ($this->sysset['agent_level'][$this->merchinfo['agent_level']]['type'] == 1) {
                    $map['m.province'] = $this->merchinfo['province'];
                    $map['m.city'] = $this->merchinfo['city'];
                    $map['m.area'] = $this->merchinfo['area'];
                    $map['m.agent_id'] = $this->merchinfo['id'];
                }
                if ($this->sysset['agent_level'][$this->merchinfo['agent_level']]['type'] == 2) {
                    $map['m.province'] = $this->merchinfo['province'];
                    $map['m.city'] = $this->merchinfo['city'];
                    $map['m.agent_id'] = $this->merchinfo['id'];
                }
                if ($this->sysset['agent_level'][$this->merchinfo['agent_level']]['type'] == 3) {
                    $map['m.province'] = $this->merchinfo['province'];
                    $map['m.agent_id'] = $this->merchinfo['id'];
                }
            } else {
                $map['m.agent_id'] = $this->merchinfo['id'];
            }
        } else {
            $map['s.member_id'] = $this->merchinfo['id'];
        }
        if (isset($_GPC['sale_id']) && $_GPC['sale_id'] != '') {
            $map['s.id'] = $_GPC['sale_id'];
        }
        if (isset($_GPC['member_id']) && $_GPC['member_id'] != '') {
            $map['s.member_id'] = $_GPC['member_id'];
        }
        $db = Db::name('member_video')
                ->alias('v')
                ->join('sale s', 'v.sale_id=s.id')
                ->join('member m', 's.member_id=m.id')
                ->field('v.*,s.title,s.poi_name')
                ->where($map);

        $count = $db->count();

        $data = $db->order('v.createtime DESC')->limit(($page - 1) * $limit, $limit)->select();
        jsonp($data, $count);
    }

    /**
     * 更新视频数据
     * @return \think\response\Jsonp
     */
    public function getdata() {
        $_GPC = $this->data;
        $id = $_GPC['id'];
        $data = Db::name('member_video')->where(['id'=>$id])->field(['item_id','member_id'])->find();
        $member = Member::where(['id'=>$data['member_id']])->field(['openid','access_token'])->find();
        $udata = array('status' => 0, 'updatetime' => time());
        $vdata = Dy::syncVideo($member['openid'],$member['access_token'],$data['item_id']);
        if ($vdata['extra']['error_code'] == 0) {
            $udata['avg_play_duration'] = $vdata['data']['result']['avg_play_duration'];
            $udata['total_comment'] = $vdata['data']['result']['total_comment'];
            $udata['total_like'] = $vdata['data']['result']['total_like'];
            $udata['total_play'] = $vdata['data']['result']['total_play'];
            $udata['total_share'] = $vdata['data']['result']['total_share'];
            $udata['status'] = 1;
        }
        $res = Db::name('member_video')->where(['id'=>$id])->update($udata);
        if(!$res) throw new ValidateException('视频数据更新失败！');
        return jsonp([]);
    }

    /**
     * 批量更新视频数据
     */
    public function syncVideoAll(){
        $_GPC = $this->data;
        $id = $_GPC['id'];
        $data = Db::name('member_video')->where('id','in',$id)->field(['item_id','member_id','id'])->select();
        $errNum = 0;
        $num = 0;
        if(!empty($data)){
            foreach ($data as $datum){
                $member = Member::where(['id'=>$datum['member_id']])->field(['openid','access_token'])->find();
                $udata = array('status' => 0, 'updatetime' => time());
                $vdata = Dy::syncVideo($member['openid'],$member['access_token'],$datum['item_id']);
                if ($vdata['extra']['error_code'] == 0) {
                    $udata['avg_play_duration'] = $vdata['data']['result']['avg_play_duration'];
                    $udata['total_comment'] = $vdata['data']['result']['total_comment'];
                    $udata['total_like'] = $vdata['data']['result']['total_like'];
                    $udata['total_play'] = $vdata['data']['result']['total_play'];
                    $udata['total_share'] = $vdata['data']['result']['total_share'];
                    $udata['status'] = 1;
                }
                $res = Db::name('member_video')->where(['id'=>$datum['id']])->update($udata);
                if(!$res){
                    $errNum+=1;
                }else{
                    $num+=1;
                }
            }
        }
        return jsonp([],'更新成功：'.$num.'，失败：'.$errNum);
    }

    public function del() {

        $_GPC = input();
        $id = $_GPC['id'];
        if ($this->request->isPost()) {
            $result = Db::name('member_video')->whereIn('id', $id)->delete();

            if (!empty($result) || $result === 0) {
                json('success', 0, '删除成功');
            } else {
                json('error', 1, '删除失败');
            }
        }
        json('', 1, 'error');
    }

}
